package com.pro.controller;

import com.pro.pojo.JsUnseal;
import com.pro.pojo.JsonData;
import com.pro.service.JsUnsealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/JsUnseal")
public class JsUnsealController {

    @Autowired
    private JsUnsealService jsUnsealService;

    //查看前台用户解封记录
    @RequestMapping("/getJsUnsealLog")
    @ResponseBody
    public JsonData getJsUnsealLog(JsUnseal jsUnseal){
        List<JsUnseal> jsUnsealLog = jsUnsealService.getJsUnsealLog(jsUnseal);
        return jsUnsealLog!=null?JsonData.buildSuccess(jsUnsealLog):JsonData.buildError("服务器出错了");
    }

    //高级查看前台用户解封记录
    @RequestMapping("/getJsUnsealLogRetrieval")
    @ResponseBody
    public JsonData getJsUnsealLogRetrieval(JsUnseal jsUnseal){
        List<JsUnseal> jsUnsealLog = jsUnsealService.getJsUnsealLogRetrieval(jsUnseal);
        return jsUnsealLog!=null?JsonData.buildSuccess(jsUnsealLog):JsonData.buildError("服务器出错了");
    }

    //删除解封日志
    @RequestMapping("/deleteUnsealLog")
    @ResponseBody
    public JsonData deleteUnsealLog(JsUnseal jsUnseal){
        boolean b = jsUnsealService.deleteUnsealLog(jsUnseal);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //添加解封记录
    @RequestMapping(value = "/addUnsealUser")
    @ResponseBody
    public JsonData addUnsealUser(JsUnseal jsUnseal){
        boolean addUnsealUser = jsUnsealService.addUnsealUser(setEndTime(jsUnseal));
        return addUnsealUser ? JsonData.buildSuccess("成功") : JsonData.buildError("服务器出错了");
    }
    private JsUnseal setEndTime(JsUnseal jsUnseal){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime lf = LocalDateTime.now();
        String endDate = df.format(lf.plusDays(jsUnseal.getDays()));
        jsUnseal.setEndDate(endDate);
        return jsUnseal;
    }

    //根据账号名称、所在部门、解封日期查找解封日志
    @RequestMapping(value = "/getJsUnsealLogs")
    @ResponseBody
    public JsonData getJsUnsealLogs(JsUnseal jsUnseal){
        List<JsUnseal> jsUnsealLogs = jsUnsealService.getJsUnsealLogs(jsUnseal);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",jsUnsealLogs);
        map.put("code",0);
        map.put("msg","");
        return map.get("data") != null ? JsonData.buildSuccess(map) : JsonData.buildError("服务器出错了");
    }
}
